const express = require('express');

// 通道相关的API路由
function setupChannelRoutes(pool) {
  const router = express.Router();
  
  // 获取通道列表
  router.get('/api/channels', async (req, res) => {
    try {
      const { config_id } = req.query;
      
      let query = 'SELECT * FROM Channels';
      let params = [];
      
      if (config_id) {
        query += ' WHERE config_id = $1';
        params.push(config_id);
      }
      
      query += ' ORDER BY id';
      
      const result = await pool.query(query, params);
      res.json(result.rows);
    } catch (error) {
      console.error('获取通道列表错误:', error);
      res.status(500).json({ error: error.message });
    }
  });

  // 获取单个通道信息
  router.get('/api/channels/:id', async (req, res) => {
    try {
      const { id } = req.params;
      const result = await pool.query('SELECT * FROM Channels WHERE id = $1', [id]);
      
      if (result.rows.length === 0) {
        return res.status(404).json({ error: '通道不存在' });
      }
      
      res.json(result.rows[0]);
    } catch (error) {
      console.error('获取通道信息错误:', error);
      res.status(500).json({ error: error.message });
    }
  });

  return router;
}

module.exports = setupChannelRoutes;
